
import React, { PureComponent, Fragment ,Component} from 'react';
import { connect } from 'dva';
import { routerRedux } from 'dva/router';
import {
  Row, Col, Card, Form, Input, Select, Icon, Button, Modal, message, Badge, Divider, Tag, Popconfirm,
} from 'antd';
import StandardTable from 'components/StandardTable';
import PageHeaderLayout from '../../layouts/PageHeaderLayout';
import { Map } from 'react-amap';
import styles from './Activity.less';
import { imgPrex } from '../../utils/constData';
import svgpath from 'svgpath';
const FormItem = Form.Item;
const { Option } = Select;
let actApplyList =['普通会员,','借阅会员,','vip会员']
@connect(({ activity, loading }) => ({
  activity,
  pagination: activity.pagination,
  loading: loading.models.activity,
}))
@Form.create()
export default class Activity extends PureComponent {
  state = {
    selectedRows: [],
  };

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'activity/fetchActivityList',
      payload: { 'pageNum': 1, 'pageSize': 10 }
    });
  }



  handleFormReset = () => {
    const { form, dispatch } = this.props;
    form.resetFields();
    const params = {
      pageNum: 1,
      pageSize: 10,
    };
    dispatch({
      type: 'activity/fetchActivityList',
      payload: params,
    });
  };

 
  handleSearch() {
    const { dispatch, form } = this.props;
    form.validateFields((err, fieldsValue) => {
      if (err) return;

      const values = {
        ...fieldsValue,
        pageNum: 1,
        pageSize:  10,
      };
      dispatch({
        type: 'activity/fetchActivityList',
        payload: values,
      });
    });
  };

deleteActivity(actid,e){
  const params={
    storeId:storeid
  }
  this.props.dispatch({
    type: 'activity/delActList',
    payload: params,
    callback: () => this.callback()
  });
}


  upOne(actId) {
    let recordList = [];
    recordList.push({ actId: actId })
    this.props.dispatch({
      type: 'activity/upActList',
      payload: { actList: recordList },
      callback: () => this.callback()
    });
  }

  downOne(actId) {
    let recordList = [];
    recordList.push({ actId: actId })
    this.props.dispatch({
      type: 'activity/downActList',
      payload: { actList: recordList },
      callback: () => this.callback()
    });
  }

  handleStandardTableChange = (pagination) => {
    const { dispatch } = this.props;

    const params = {
      pageNum: pagination.current,
      pageSize: pagination.pageSize,
      ...this.props.form.getFieldsValue(),
    };

    dispatch({
      type: 'activity/fetchActivityList',
      payload: params,
    });
  };

handleSelectRows = rows => {
    this.setState({
      selectedRows: rows,
    });
  };

compare(property){
    return function(a,b){
        var value1 = a[property];
        var value2 = b[property];
        //return value1 - value2;
        return value2 - value1;
    }
}

  changeActStatus(type) {
    let list = this.state.selectedRows;
    if (list && list.length > 0) {
      let recordList = [];
      for (let i in list) {
        let one = { actId: list[i].actId }
        recordList.push(one)
      }
      console.log(recordList)
      if (type == "up") {  //上架
        if (recordList) {
          this.props.dispatch({
            type: 'activity/upActList',
            payload: { actList: recordList },
            callback: () => this.callback()
          });
        }
      } else if (type == "down") {
        if (recordList) {
          this.props.dispatch({
            type: 'activity/downActList',
            payload: { actList: recordList },
            callback: () => this.callback()
          });
        }
      } else if (type == "delete") {
        if (recordList) {
          this.props.dispatch({
            type: 'activity/delActList',
            payload: { actList: recordList },
            callback: () => this.callback()
          });
        }
      }
    } else {
      message.error('请选择内容')
    }
  }

  callback() {
    this.handleSearch()
  }




  renderAdvancedForm() {
    const { form } = this.props;
    const { getFieldDecorator } = form;
    return (
      <Form layout="inline">
      <div style={{ overflow: 'hidden' }}>
          <span style={{ float: 'right', marginBottom: 24 }}>
            <Button onClick={this.handleFormReset} style={{ border:'none' ,color:'#80829e'}}>
              重置
            </Button>
            <Button style={{ marginLeft: 12 }} className={styles.serachButton} type="primary" htmlType="submit" onClick={() => this.handleSearch()}>
              查询
            </Button>
          </span>
        </div>
        <Row style={{border:'1px dashed #ccc',padding:'30px 10px 0',marginBottom:20,position:'relative'}} gutter={{ md: 8, lg: 24, xl: 48 }}>
          <div className={styles.absoluteposition}>活动信息</div>
          <Col md={8} sm={24}>
            <FormItem label="活动名称">
              {getFieldDecorator('actName')(<Input placeholder="请输入" />)}
            </FormItem>
          </Col>
          <Col md={8} sm={24}>
            <FormItem label="状态">
              {getFieldDecorator('actSts', { initialValue: 'undefined' })(
                <Select style={{ width: '100%' }}>
                  <Option value="undefined">全部</Option>
                  <Option value="1">上架</Option>
                  <Option value="2">下架</Option>
                </Select>
              )}
            </FormItem>
          </Col>
        </Row>

        
      </Form>
    );
  }


  render() {
    const { activity, loading} = this.props;
    const {selectedRows} =this.state
    const tableData = {}
    let pageinfo = {}
    if(activity.actListData.list != null){
        tableData['list'] = activity.actListData.list
    }
    pageinfo['pageNum'] =  activity.actPagination.pageNum
    pageinfo['pageSize'] =activity.actPagination.pageSize
    pageinfo['pages'] =activity.actPagination.pages
    pageinfo['total'] =activity.actListData.total
    tableData['pagination'] = activity.actPagination;

    const columns = [
      {
        title: '活动Id',
        dataIndex: 'actId',
        key: 'actId',
         render:(text,record) => (
          <Fragment> 
            <a onClick={() => this.props.dispatch(routerRedux.push({ pathname: '/activity/actdetail/' + record.actId }))}>{record.actId}</a>
          </Fragment>
        ),
      },
      {
        title: '封面',
        dataIndex: 'actImage',
        key: 'actImage',
        render(text, record) {
          return <img src={imgPrex + record.actImg} alt={record.title} style={{ width: 30, height: 30 }} />;
        },
      },
      {
        title: '活动名称',
        dataIndex: 'actName',
        key: 'actName',
      },
      {
        title: '活动门店',
        dataIndex: 'storeName',
        key: 'storeName',
      },
      {
        title: '状态',
        dataIndex: 'actSts',
        key: 'actSts',
        render(val) {
          return val == "1" ? "上架" :  val == "2" ? <span style={{ color: 'red' }}>下架</span> :null
        },
      },
      {
        title: '适用人群',
        dataIndex: 'actApply',
        key: 'actApply',
        render(text,record) {
          return <div>{record.actApply.split(',').map(item=>{ return actApplyList[item]})}</div>
        },
      },
      {
        title: '权重',
        dataIndex: 'weight',
        key: 'weight',

      },
      {
        title: '购买人数',
        dataIndex: 'makeCount',
        key: 'makeCount',
      },
      {
        title: '操作',
        key: '操作',
        render: (text, record) => (
          <Fragment>
            <a onClick={() => this.props.dispatch(routerRedux.push({ pathname: '/activity/editactivity/' + record.actId }))}>编辑</a>
            <Divider type="vertical" />
            {record.actSts == "1" ? <a onClick={() => this.downOne(record.actId)}> <span style={{ color: '#8E8E8E' }}>下架</span></a> : record.actSts == "2" ? <a onClick={() => this.upOne(record.actId)}>上架</a> : null}
          </Fragment>
        ),
      },
    ];


    return (
      <PageHeaderLayout title="" className={styles.storeStyle}>
        <Card bordered={false}>
          <div className={styles.tableList}>
            <div className={styles.tableListForm}>{this.renderAdvancedForm()}</div>
            <div className={styles.tableListOperator}>
              <Button icon="plus" type="primary" onClick={() => this.props.dispatch(routerRedux.push({ pathname: '/activity/editactivity/new' }))}>
                新增
              </Button>
              {selectedRows.length > 0 && (
                <span>
                  <Popconfirm title={"你确定要上架选择的" + this.state.selectedRows.length + "个选项吗?"} placement="bottom" onConfirm={() => this.changeActStatus("up")} onCancel={() => { }} okText="确定" cancelText="取消">
                    <Button type="primary" icon="upload" >
                      上架
                  </Button>
                  </Popconfirm>
                  <Popconfirm title={"你确定要下架选择的" + this.state.selectedRows.length + "个选项吗?"} placement="bottom" onConfirm={() => this.changeActStatus("down")} onCancel={() => { }} okText="确定" cancelText="取消">
                    <Button type="primary" icon="download">
                      下架
                  </Button>
                  </Popconfirm>
                  <Popconfirm title={"你确定要上架选择的" + this.state.selectedRows.length + "个选项吗?"} placement="bottom" onConfirm={() => this.changeActStatus("delete")} onCancel={() => { }} okText="确定" cancelText="取消">
                    <Button type="danger" icon="delete">
                      删除
                  </Button>
                  </Popconfirm>

                </span>
              )}
            </div>
            <StandardTable
              selectedRows={selectedRows}
              loading={loading}
              data={tableData}
              columns={columns}
              onSelectRow={this.handleSelectRows}
              onChange={this.handleStandardTableChange}
              rowKey="actId"
            />
          </div>
        </Card>
        
            
      </PageHeaderLayout>
    );
  }
}
